package com.xwltz.database.page;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwltz.core.constants.HttpStatus;
import com.xwltz.core.utils.ConvertUtils;
import com.xwltz.core.utils.web.ServletUtils;
import com.xwltz.core.utils.StringUtils;

import java.util.List;

/**
 * @description PageUtils
 * @information on version 1.0 be created by @xwltz at 2021/12/28 14:19
 */
public class PageUtils {
	/**
	 * 当前记录起始索引
	 */
	public static final String PAGE_NO = "pageNo";

	/**
	 * 每页显示记录数
	 */
	public static final String PAGE_SIZE = "pageSize";

	/**
	 * 排序列
	 */
	public static final String ORDER_BY_COLUMN = "sortColumn";

	/**
	 * 排序的方向 "desc" 或者 "asc".
	 */
	public static final String IS_ASC = "sortType";

	/**
	 * 当前记录起始索引 默认值
	 */
	public static final int DEFAULT_PAGE_NO = 1;

	/**
	 * 每页显示记录数 默认值 默认查全部
	 */
	public static final int DEFAULT_PAGE_SIZE = 10;

	/**
	 * 构建 plus 分页对象
	 *
	 * @param <T> domain 实体
	 * @param <K> vo 实体
	 * @return 分页对象
	 */
	public static <T, K> PagePlus<T, K> buildPagePlus() {
		Integer pageNo = ServletUtils.getParameterToInt(PAGE_NO, DEFAULT_PAGE_NO);
		Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
		String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN);
		String isAsc = ServletUtils.getParameter(IS_ASC);
		if (pageNo <= 0) {
			pageNo = DEFAULT_PAGE_NO;
		}
		PagePlus<T, K> page = new PagePlus<>(pageNo, pageSize);
		OrderItem orderItem = buildOrderItem(orderByColumn, isAsc);
		if (ObjectUtil.isNotNull(orderItem)) {
			page.addOrder(orderItem);
		}
		return page;
	}

	public static <T> Page<T> buildPage() {
		return buildPage(null, null);
	}

	/**
	 * 构建 MP 普通分页对象
	 *
	 * @param <T> domain 实体
	 * @return 分页对象
	 */
	public static <T> Page<T> buildPage(String defaultOrderByColumn, String defaultIsAsc) {
		Integer pageNo = ServletUtils.getParameterToInt(PAGE_NO, DEFAULT_PAGE_NO);
		Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE, DEFAULT_PAGE_SIZE);
		String orderByColumn = ServletUtils.getParameter(ORDER_BY_COLUMN, defaultOrderByColumn);
		String isAsc = ServletUtils.getParameter(IS_ASC, defaultIsAsc);
		if (pageNo <= 0) {
			pageNo = DEFAULT_PAGE_NO;
		}
		Page<T> page = new Page<>(pageNo, pageSize);
		OrderItem orderItem = buildOrderItem(orderByColumn, isAsc);
		if (ObjectUtil.isNotNull(orderItem)) {
			page.addOrder(orderItem);
		}
		return page;
	}

	private static OrderItem buildOrderItem(String orderByColumn, String isAsc) {
		// 兼容前端排序类型
		if ("ascending".equals(isAsc)) {
			isAsc = "asc";
		} else if ("descending".equals(isAsc)) {
			isAsc = "desc";
		}
		if (StringUtils.isNotBlank(orderByColumn)) {
			String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
			orderBy = StringUtils.toUnderScoreCase(orderBy);
			if ("asc".equals(isAsc)) {
				return OrderItem.asc(orderBy);
			} else if ("desc".equals(isAsc)) {
				return OrderItem.desc(orderBy);
			}
		}
		return null;
	}

	public static <T, K> TableList<K> buildDataInfo(PagePlus<T, K> page) {
		TableList<K> rspData = new TableList<>();

		rspData.setCode(HttpStatus.HTTP_OK);
		rspData.setMsg("查询成功");
		rspData.setData(page.getRecordsVo());
		rspData.setTotalSize(page.getTotal());
		rspData.setTotalPageCount(ConvertUtils.toInt(page.getPages(), 0));
		rspData.setPageNo(ConvertUtils.toInt(page.getCurrent(), 0));
		rspData.setSize(ConvertUtils.toInt(page.getSize(), 0));
		rspData.setIsSuccess(true);
		return rspData;
	}

	public static <T> TableList<T> buildDataInfo(Page<T> page) {
		TableList<T> rspData = new TableList<>();

		rspData.setCode(HttpStatus.HTTP_OK);
		rspData.setMsg("查询成功");
		rspData.setData(page.getRecords());
		rspData.setTotalSize(page.getTotal());
		rspData.setTotalPageCount(ConvertUtils.toInt(page.getPages(), 0));
		rspData.setPageNo(ConvertUtils.toInt(page.getCurrent(), 0));
		rspData.setSize(ConvertUtils.toInt(page.getSize(), 0));
		rspData.setIsSuccess(true);
		return rspData;
	}

	public static <T> TableList<T> buildDataInfo(List<T> list) {
		TableList<T> rspData = new TableList<>();
		rspData.setCode(HttpStatus.HTTP_OK);
		rspData.setMsg("查询成功");
		rspData.setData(list);
		rspData.setTotalSize(ConvertUtils.toLong(list.size()));
		return rspData;
	}

	public static <T> TableList<T> buildDataInfo() {
		TableList<T> rspData = new TableList<>();
		rspData.setCode(HttpStatus.HTTP_OK);
		rspData.setMsg("查询成功");
		return rspData;
	}
}
